import { BaseWindow } from "../../../component/window/BaseWindow";
import { ILayer } from "../../../mvc/interfaces/ILayer";
import { FriendInfo } from "../../model/FriendInfo";
import { Language } from "../../../../extend/language/Language";
import { WindowEvent } from "../../../component/window/WindowEvent";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { Cache } from "../../cache/Cache";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { ObjCreate } from "../common/ObjCreate";
import { MarryNotInviteFriendCell } from "./cellRenderer/MarryNotInviteFriendCell";
import { MarryInvitedFriendCell } from "./cellRenderer/MarryInvitedFriendCell";
import { MsgManager } from "../../manager/MsgManager";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { QuickBuyWindow } from "../common/QuickBuyWindow";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { StringHelper } from "../../../../com/gengine/utils/StringHelper";
import { MarryWeddingConfig } from "../../resource/MarryWeddingConfig";
import { MarryFriendData } from "./defin/MarryFriendData";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { MarryInvitedData } from "./defin/MarryInvitedData";
import { ObjTeamCreate } from "../common/ObjTeamCreate";
import { Global } from "../../../../com/gengine/global/Global";
import { SoundManager } from "../../../common/sound/SoundManager";
import { SoundTypeConst } from "../../../common/sound/SoundTypeConst";
import { Alert } from "../../../../com/mui/controls/Alert";
import { GTileList } from "../../../../com/mui/controls/GTileList";
import { GTextInput } from "../../../../com/mui/controls/GTextInput";
import { GCheckBox } from "../../../../com/mui/controls/GCheckBox";
import { GButton } from "../../../../com/mui/controls/GButton";
import { Vector } from "../../../../__AS3__/vec/Vector";
type int = number;
//class MarryInviteGuests
    
    export  class MarryInviteGuests extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this._aryInvitedGuest = new Array<any>();
            this._vcFriendInfo = new Vector.<FriendInfo>();
            this._aryAddToRight = new Array<any>();
            setSize(483, 380);
            title = Language.getString(43571);
            titleHeight = 26;
            this.addEventListener(WindowEvent.SHOW, this.onShow);
            return;
        }

        /* internal  */onShow(arg1: WindowEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MarryGetInvitedGuest));
            if (Cache.instance.marry.guestInfo) 
            {
                this.updateCanInvitedNum(Cache.instance.marry.guestInfo.canInviteGuestNum);
                this.updateCanUseInviteCardNum(Cache.instance.marry.guestInfo.canUseInviteCardNum);
            }
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc3=null;
            super.createChildren();
            var loc1=GlobalStyle.textFormat1;
            loc1.leading = 24;
            loc1.leading = 6;
            var loc2=GlobalStyle.textFormat1;
            loc2.color = 65280;
            loc2.leading = 7;
            this._tfTips = ObjCreate.createTextField("", 26, 41, 415, 60, this, loc1);
            this._tfTips.multiline = true;
            this._tfTips.wordWrap = true;
            this._tfTips.htmlText = "";
            this._tfCanInviteNum = ObjCreate.createTextField(Language.getString(43573), 26, 81, 200, 20, this);
            this._tfCanUseInviteNum = ObjCreate.createTextField(Language.getString(43612), 300, 81, 210, 20, this);
            ObjCreate.createTextField(Language.getString(43574), 20, 113, 150, 20, this, GlobalStyle.textFormat6);
            ObjCreate.createBg(19, 132, 168, 235, this);
            this._tileListFriends = ObjCreate.createTileList(26, 138, 158, 225, this);
            this._tileListFriends.rowHeight = 21;
            this._tileListFriends.columnWidth = 164;
            this._tileListFriends.setStyle("cellRenderer", MarryNotInviteFriendCell);
            this._btnInviteOver = ObjCreate.createGButton(Language.getString(43575), 211, 288, 70, 26, this, "ButtonLarge");
            this._cbAutoInvite = ObjCreate.createCheckBox(Language.getString(43661), 197, 314, 120, 28, this);
            this._cbAutoInvite.selected = true;
            loc3 = ObjCreate.createTextField(Language.getString(43576), 194, 166, 110, 130, this, loc2);
            loc3.multiline = true;
            loc3.wordWrap = true;
            this._nameInput = ObjCreate.createGTextInput(303, 108, 100, 22, this);
            this._nameInput.maxChars = 7;
            this._btnAdd = ObjCreate.createGButton(Language.getString(43613), 410, 106, 52, 26, this);
            ObjCreate.createBg(303, 134, 161, 233, this);
            this._tileListInvited = ObjCreate.createTileList(305, 138, 157, 227, this);
            this._tileListInvited.rowHeight = 23;
            this._tileListInvited.columnWidth = 150;
            this._tileListInvited.setStyle("cellRenderer", MarryInvitedFriendCell);
            this.updateFriends();
            this._btnInviteOver.addEventListener(flash.events.MouseEvent.CLICK, this.onClickInvite);
            this._btnAdd.addEventListener(flash.events.MouseEvent.CLICK, this.onClickAdd);
            return;
        }

        /* internal  */onClickInvite(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._isBtnInvite = true;
            this.beginInvite();
            return;
        }

        public beginInvite(): void
        {
            if (!this._isBtnInvite) 
            {
                return;
            }
            if (this._aryAddToRight.length == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(43577));
                return;
            }
            if (this._canInviteNum < this._aryAddToRight.length) 
            {
                if (this._cbAutoInvite.selected) 
                {
                    if (Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isInvitation).length > 0) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.MarryAutoUseInvite, this._aryAddToRight.length - this._canInviteNum));
                        return;
                    }
                }
                else if (Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isInvitation).length > 0) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(43665));
                    return;
                }
                QuickBuyWindow.instance.updateWindow(ECategory._ECategoryProp, EProp._EPropInvitation);
                QuickBuyWindow.instance.updateDefaultAmount(this._aryAddToRight.length - this._canInviteNum);
                QuickBuyWindow.instance.show();
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.MarryInviteNow, this._aryAddToRight));
            this._isBtnInvite = false;
            return;
        }

        /* internal  */onClickAdd(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=this._nameInput.text;
            if (StringHelper.trim(loc1) == "") 
            {
                MsgManager.showRollTipsMsg(Language.getString(43614));
                return;
            }
            if (loc1.length > 7) 
            {
                MsgManager.showRollTipsMsg(Language.getString(43615));
                return;
            }
            if (this._aryInvitedGuest.indexOf(loc1) > -1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(43616));
                return;
            }
            if (this._aryAddToRight.indexOf(loc1) > -1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(43617));
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.MarrySearchPlayerInvite, loc1));
            return;
        }

        public updateInvitedGuest(arg1: Array<any>): void
        {
            this._aryInvitedGuest = arg1;
            this.updateView();
            return;
        }

        public addPeople(arg1: string): void
        {
            var loc1=null;
            if (this._aryAddToRight.indexOf(arg1) == -1) 
            {
                loc1 = Cache.instance.marry.playerMarry;
                if (loc1 && loc1.mate && loc1.mate.name == arg1) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(43641));
                    return;
                }
                if (Cache.instance.role.entityInfo.name == arg1) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(43642));
                    return;
                }
                this._aryAddToRight.push(arg1);
                this.updateView();
                if (this._nameInput.text == arg1) 
                {
                    this._nameInput.text = "";
                }
            }
            return;
        }

        public cancelPeople(arg1: string): void
        {
            var loc1=this._aryAddToRight.indexOf(arg1);
            if (loc1 > -1) 
            {
                this._aryAddToRight.splice(loc1, 1);
                this.updateView();
            }
            return;
        }

        public updateFriends(): void
        {
            this._vcFriendInfo = Cache.instance.friend.getFriendList();
            this.updateView();
            return;
        }

        /* internal  */updateView(): void
        {
            var loc7=null;
            var loc8=null;
            var loc9=null;
            var loc10=null;
            var loc11=null;
            var loc12=null;
            var loc13=null;
            var loc14=0;
            var loc1="";
            var loc2=0;
            if (Cache.instance.marry.playerMarry) 
            {
                loc1 = (loc10 = MarryWeddingConfig.instance.getWeddingByType(Cache.instance.marry.weddingStatus.weddingType)).name;
                loc2 = loc10.guest;
            }
            this._tfTips.htmlText = Language.getStringByParam(43578, loc1, loc2.toString());
            var loc3=new Array<any>();
            var loc4=0;
            while (loc4 < this._vcFriendInfo.length) 
            {
                loc12 = (loc11 = this._vcFriendInfo[loc4]).friendPlayerInfo.playerName;
                if (Cache.instance.marry.playerMarry && Cache.instance.marry.playerMarry.mate) 
                {
                    if (loc12 != Cache.instance.marry.playerMarry.mate.name) 
                    {
                        if (Cache.instance.role.entityInfo.name != loc12) 
                        {
                            loc13 = new MarryFriendData(loc12, loc11.friendPlayerInfo.camp);
                            if (this._aryInvitedGuest.indexOf(loc12) > -1) 
                            {
                                loc13.isInvited = true;
                            }
                            if (this._aryAddToRight.indexOf(loc12) > -1) 
                            {
                                loc13.isAddToRight = true;
                            }
                            loc3.push(loc13);
                        }
                    }
                }
                else if (Cache.instance.role.entityInfo.name != loc12) 
                {
                    loc13 = new MarryFriendData(loc12, loc11.friendPlayerInfo.camp);
                    if (this._aryInvitedGuest.indexOf(loc12) > -1) 
                    {
                        loc13.isInvited = true;
                    }
                    if (this._aryAddToRight.indexOf(loc12) > -1) 
                    {
                        loc13.isAddToRight = true;
                    }
                    loc3.push(loc13);
                }
                ++loc4;
            }
            var loc5=new DataProvider(loc3);
            this._tileListFriends.dataProvider = loc5;
            this._tileListFriends.drawNow();
            var loc6=new Array<any>();
            var loc15=0;
            var loc16=this._aryInvitedGuest;
            for(loc7 of loc16) 
            {
                loc6.push(new MarryInvitedData(loc7, true));
                if (!((loc14 = this._aryAddToRight.indexOf(loc7)) > -1)) 
                {
                    continue;
                }
                this._aryAddToRight.splice(loc14, 1);
            }
            loc15 = 0;
            loc16 = this._aryAddToRight;
            for(loc8 of loc16) 
            {
                loc6.push(new MarryInvitedData(loc8, false));
            }
            loc9 = new DataProvider(loc6);
            this._tileListInvited.dataProvider = loc9;
            this._tileListInvited.validateNow();
            return;
        }

        public updateCanInvitedNum(arg1: int): void
        {
            this._canInviteNum = arg1;
            ObjTeamCreate.setTextFieldKeyValue(this._tfCanInviteNum, Language.getString(43579), arg1.toString());
            return;
        }

        public updateCanUseInviteCardNum(arg1: int): void
        {
            ObjTeamCreate.setTextFieldKeyValue(this._tfCanUseInviteNum, Language.getString(43612), arg1.toString());
            return;
        }

        protected /* override */ closeBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var onClose: Function;

            var loc1;
            e = arg1;
            Global.stage.focus = Global.stage;
            SoundManager.instance.soundPlay(SoundTypeConst.UIclose);
            if (this._aryAddToRight.length > 0) 
            {
                onClose = (arg1: int): void
                {
                    if (arg1 != Alert.OK) 
                    {
                        hide();
                    }
                    else 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.MarryInviteNow, _aryAddToRight));
                    }
                    return;
                }
                Alert.okLabel = Language.getString(43618);
                Alert.calcelLabel = Language.getString(43619);
                Alert.show(Language.getString(43620), null, Alert.OK | Alert.CANCEL, null, onClose);
            }
            else 
            {
                hide();
            }
            return;
        }

        /* internal */ /* var */_tfTips: Object /* flash.text.TextField */;

        /* internal */ /* var */_tfCanInviteNum: Object /* flash.text.TextField */;

        /* internal */ /* var */_tfCanUseInviteNum: Object /* flash.text.TextField */;

        /* internal */ /* var */_tileListFriends: GTileList;

        /* internal */ /* var */_tileListInvited: GTileList;

        /* internal */ /* var */_nameInput: GTextInput;

        /* internal */ /* var */_canInviteNum: int=0;

        /* internal */ /* var */_cbAutoInvite: GCheckBox;

        /* internal */ /* var */_btnInviteFriend: GButton;

        /* internal */ /* var */_btnInviteOver: GButton;

        /* internal */ /* var */_btnAdd: GButton;

        /* internal */ /* var */_isBtnInvite: Boolean=false;

        /* internal */ /* var */_aryInvitedGuest: Array<any>;

        /* internal */ /* var */_vcFriendInfo: Vector.<FriendInfo>;

        /* internal */ /* var */_aryAddToRight: Array<any>;
    }
